package com.cyj.dream.test.controller;

import cn.hutool.core.date.DateUtil;
import com.cyj.dream.core.constant.Constant;
import com.cyj.dream.core.util.user.UUIDUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 消息推送接口 https://blog.csdn.net/qq_35387940/article/details/100514134
 * 1. 再写个简单的接口进行消息推送（根据需求也可以改为定时任务等等，具体看需求），SendMessageController.java：
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.test.controller
 * @Author: ChenYongJia
 * @CreateTime: 2021-10-18 16:04
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@RestController
public class SendMessageController {

    /**
     * 使用RabbitTemplate,这提供了接收/发送等等方法
     */
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUIDUtils.getUUIDNoHave());
        String messageData = "这是一条测试消息, 你好啊骚年！";
        Map<String, Object> map = this.getMap(messageId, messageData);
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
        return "ok";
    }

    // ================================= 生产者使用 Topic 话题模式 ================================

    @GetMapping("/sendTopicMessage1")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUIDUtils.getUUIDNoHave());
        String messageData = "message: M A N ";
        Map<String, Object> manMap = this.getMap(messageId, messageData);
        rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
        return "ok";
    }

    @GetMapping("/sendTopicMessage2")
    public String sendTopicMessage2() {
        String messageId = String.valueOf(UUIDUtils.getUUIDNoHave());
        String messageData = "message: woman is all ";
        Map<String, Object> womanMap = this.getMap(messageId, messageData);
        rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
        return "ok";
    }

    @GetMapping("/TestMessageAck")
    public String TestMessageAck() {
        String messageId = String.valueOf(UUIDUtils.getUUIDNoHave());
        String messageData = "message: non-existent-exchange test message ";
        Map<String, Object> map = this.getMap(messageId, messageData);
        rabbitTemplate.convertAndSend("non-existent-exchange", "TestDirectRouting", map);
        return "ok";
    }

    @GetMapping("/TestMessageAck2")
    public String TestMessageAck2() {
        String messageId = String.valueOf(UUIDUtils.getUUIDNoHave());
        String messageData = "message: lonelyDirectExchange test message ";
        Map<String, Object> map = this.getMap(messageId, messageData);
        rabbitTemplate.convertAndSend("lonelyDirectExchange", "TestDirectRouting", map);
        return "ok";
    }

    private Map<String, Object> getMap(String messageId, String messageData) {
        Map<String, Object> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", DateUtil.now());
        return map;
    }

}
